<?php

namespace App\Http\Controllers\Home;

use Illuminate\Auth\Access\Response;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class ProfileController extends Controller
{
    public function index() {
        return view('home.profile.index');
    }

    public function order() {
        return view('home.profile.order.index');
    }

    public function unrated() {
        return view('home.profile.order.unrated');
    }
    //修改个人信息
    public function info() {
        return view('home.profile.info.index');
    }
    //处理上传头像
    public function doinfo(Request $request)
    {
        // dd($request);
        $this -> validate($request,[
                'username' => 'required',
                'phone' => 'required|max:11|min:11',
                'email' => 'required|email',
            ],[
                'username.required' => '用户名不能为空',
                'phone.required' => '手机号不能为空',
                'phone.max' => '手机号必须是11位的',
                'phone.min' => '手机号必须是11位的',
                'email.required' => '邮箱不能为空',
                'email.email' => '邮箱格式不正确',
            ]);

        //处理好数据
        $data = $request -> except('_token');
        $time = date("Y-m-d H:i:s");
        $data['updated_at'] = $time;
        // session(['master' => $res]);

        //上传图片
        if($request -> hasFile('image'))
        {
            
            if($request -> file('image') -> isValid())
            {
                //获取扩展名
            
                $suffix = $request -> file('image') -> getClientOriginalExtension();
                //随机名称
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                $request ->file('image') -> move('./uploads', $fileName);
                //处理image字段
                $data['image'] = '/uploads/'.$fileName;

            }   
        }

        // session()->push('master.image', $data['image']);
        // dd(session() -> all());
       
        // 执行更新
        $res = DB::table('users') -> where('id', $request -> input('id'))-> update($data);
        //判断
        if($res)
        {
            //在查一遍数据库,更新session
            $newres = DB::table('users')-> where('id', $request -> input('id')) -> first();
            if($newres)
            {
                session(['master' => $newres]);
                return redirect('/profile/info') -> with(['info' => '更新成功']);
            }else
            {
               return back() -> with(['info' => '更新失败']);  
            }
           
        }else
        {
            return back() -> with(['info' => '更新失败']);
        }

    }


    public function address() {
        return view('home.profile.address.index');
    }

    public function addAddress(Request $request) {
        $address = $request->address;
        $data = DB::table('addresses')->where('address', 'like', '%'.$address.'%')->select('address')->paginate(10);


        return response()->json($data);
    }

    public function favor() {
        return view('home.profile.favor.index');
    }

    public function layout() {
        return view('home.layout');
    }
   
    //修改密码
    public function dochange(Request $request)
    {
//        dd($resquest);
    }
}
